package xworker.swt.dataObject.datastore;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Control;
import org.xmeta.ActionContext;
import xworker.dataObject.data.DataSelector;
import xworker.dataObject.DataObject;
import xworker.dataObject.DataStore;
import xworker.dataObject.DataStoreListener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class ControlDataStoreListener extends DataSelector<DataObject> implements DisposeListener, DataStoreListener {
    Control control;
    ActionContext actionContext;
    DataStore dataStore;

    public ControlDataStoreListener(Control control, DataStore dataStore, ActionContext actionContext){
        this.actionContext = actionContext;
        this.control = control;
        this.dataStore = dataStore;

        if(dataStore != null){
            dataStore.addListener(this);
            control.addDisposeListener(this);
        }
    }

    public DataStore getDataStore() {
        return dataStore;
    }

    public Control getControl() {
        return control;
    }

    public ActionContext getActionContext() {
        return actionContext;
    }

    public void exec(Runnable runnable){
        if(!control.isDisposed()){
            control.getDisplay().asyncExec(runnable);
        }
    }

    @Override
    public void setSelectionByIds(Object... ids) {
        this.clearSelection();

        if(getAllItems() != null){
            for(DataObject dataObject : getAllItems()){
                for(Object id : ids){
                    if(dataObject.equalsByKey(id)){
                        this.addSelection(dataObject);
                    }
                }
            }
        }

        selectionChanged();
    }

    /**
     * 一般是外部设置了选择触发的事件。
     */
    abstract protected void selectionChanged();

    @Override
    public void widgetDisposed(DisposeEvent e) {
        if(dataStore != null){
            dataStore.removeListener(this);
        }
    }

    @Override
    public void clearSelection() {
        this.clearSelectedItems();

        this.selectionChanged();
    }

    @Override
    public void removeSelection(DataObject dataObject) {
        this.removeSelectedItem(dataObject);

        this.selectionChanged();
    }

    @Override
    public void removeSelectionAll(Collection<DataObject> dataObjects) {
        this.removeSelectedItemList(dataObjects);

        this.selectionChanged();
    }

    @Override
    public void addSelection(DataObject dataObject) {
        this.addSelectedItem(dataObject);

        this.selectionChanged();
    }

    @Override
    public void addSelectionAll(Collection<DataObject> dataObjects) {
        this.addSelectedItems(dataObjects);

        this.selectionChanged();
    }

    @Override
    public void setSelectionAll(List<DataObject> dataObjects) {
        this.setSelectedItems(dataObjects);

        this.selectionChanged();
    }

    @Override
    public void setSelection(DataObject dataObject) {
        this.setSelectedItem(dataObject);

        this.selectionChanged();
    }

    @Override
    public List<Object> getSelectedItemsIdList() {
        List<Object> list = new ArrayList<>();

        for(DataObject data : getSelectedItems()){
            list.add(data.getKeyValue());
        }

        return list;
    }
}
